﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Useful.GUI.HierarchyDataGridView
{
    public class DataGridViewStretchHelperTextBoxCell : DataGridViewTextBoxCell
    {
        public DataGridViewStretchTextBoxCell BaseCell;

        public DataGridViewStretchHelperTextBoxCell(DataGridViewStretchTextBoxCell baseCell)
        {
            this.BaseCell = baseCell;
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            DataGridViewStretchTextBoxCell ch = null;
            int x = cellBounds.X;

            for (int i = ColumnIndex - 1; i >= 0 && ch == null; i--)
            {
                if (DataGridView[i, rowIndex] is DataGridViewStretchTextBoxCell)
                {
                    ch = (DataGridViewStretchTextBoxCell)DataGridView[i, rowIndex];
                }

                if (DataGridView.Columns[i].Visible)
                    x -= DataGridView.Columns[i].Width;
            }

            if (ch != null)
            {
                System.Diagnostics.Debug.WriteLine(String.Format("DataGridViewStretchHelperTextBoxCell.Paint: RowIndex = {0}, ColumnIndex = {1}", this.RowIndex, this.ColumnIndex));

                Rectangle rect = new Rectangle(x, cellBounds.Y, cellBounds.Width, cellBounds.Height);

                ch.PaintCell(graphics, clipBounds, rect, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
            }
            else
                System.Diagnostics.Debug.WriteLine(String.Format("DataGridViewStretchHelperTextBoxCell.Paint: RowIndex = {0}, ColumnIndex = {1}, ch == null", this.RowIndex, this.ColumnIndex));
        }
    }
}
